<?php
	class Product extends BaseLogic{
		protected $form;
		
		function __construct($showError=TRUE){
			parent:: __construct($showError);
			$this->form=new Form();
			$this->tabName = TAB_PREFIX."product";
			$this->fieldList=Array("pid", "pname", "price", "score", "ctime", "etime");
		}
		
		function getProduct($id){
			$sqlwhere="pid".$this->sqlWhere($id);
			return $this->get($sqlwhere);
		}
		
		function addProduct($post){
			$post["ctime"]=time();
			if($this->add($post)){
				$this->messList[] = "商品添加成功.";
				return true;
			}else{
				$this->messList[] = "添加商品失败.";
				return false;
			}
		}
		
		function modProduct($postList){
			if($this->mod($postList)){
				$this->messList[] = "商品修改成功.";
				return true;
			}else{
				$this->messList[] = "商品修改失败.";
				return false;
			}
		}
		
		function delProduct($id){
			$sqlwhere="pid".$this->sqlWhere($id);
			if($this->del($sqlwhere)){
				if($this->form->delForm($id,true)){
					$this->messList[]="该商品的所有订单删除成功";
				}
				$this->messList[] = "商品删除成功.";
				return true;
			}else{
				$this->messList[] = "商品删除失败.";
				return false;
			}
		}
		
		function getAllProduct($offset,$num){
			$sql = "SELECT pid, pname, price FROM {$this->tabName} ORDER BY pid ASC LIMIT $offset, $num";
			$result=$this->mysqli->query($sql);
			while($row=$result->fetch_assoc()){
				$data[]=$row;
			}
			return $data;
		}
		
		function getRowTotal(){
			$result=$this->mysqli->query("SELECT * FROM {$this->tabName}");
			return $result->num_rows;
		}
	}
?>